package com;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;

import model.User;
import web.annotation.Method;
import web.annotation.Param;
import web.annotation.Type;
import web.jdbc.IQuerySQL;
import web.jdbc.QuerySQL;

@Type
public class UserController {
	QuerySQL<User> qs = new QuerySQL<>();
	public String username;
	
	Logger log = Logger.getLogger(UserController.class.getName());
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	public Map<String,Object> map = new HashMap<>();
	User user = new User();
	
	public String getUser() {
		String u = "[\""+user.getId()+"\",\""+user.getUsername()+"\",\""+user.getPassword()+"\",\""+user.getAddtime()+"\"]";
		return u;
	}
	
	private List<User> list;
	
	public List<User> getList() {
		return list;
	}

	String listStringUsers;
	
	public String getListStringUsers() {
		return listStringUsers;
	}
	
	//json
	String listUsers;
	public String getListUsers() {
		return listUsers;
	}
	
	@Method(value="showUser")
	public String showUser(@Param(value="username")String name,@Param(value="password")String pass){
		System.out.println("Hello:"+name+",password:"+pass);
		username = name;
		
		user.setId(1);
		user.setUsername("测试66");
		user.setPassword("1223");
		user.setAddtime(new Date());/**/
		
		User user2 = new User();
		user2.setUsername("测试55555");
		user2.setPassword("12345");
		user2.setAddtime(new Date());
		
	/*	System.out.println("=============getResultList(User.class)===============");
		List<User> lu = qs.executeSQL("select * from user").getResultList(User.class);
		System.out.println(lu);
		
		System.out.println("=============getResultList()===============");
		List<Object[]> luo = qs.executeSQL("select * from user").getResultList();
		System.out.println(Arrays.deepToString(luo.toArray()));
		
		System.out.println("=============getResultList()===============");*/
		//qs.updateData(user);
		
		
		log.log(Level.WARNING, "Logger test....");
		//user = qs.selectData(user);
		System.out.println(user);
		Long l = System.currentTimeMillis();
		qs.insertData(user2);
		
		System.out.println((System.currentTimeMillis()-l)/(60000));
		//qs.deleteData(user);
		return "WEB-INF/NewFile.jsp";
	}
	
	@Method(value="addUser",isReturnPage=true)
	public String addUser(@Param("username")String username,@Param("password")String password){
		User user = new User();
		user.setAddtime(new Date());
		user.setPassword(password);
		user.setUsername(username);
		System.out.println(user);
		qs.insertData(user);
		return showList();
	}
	
	@Method(value="updateUser",isReturnPage = true)
	public String updateUser(@Param("user")User user){
		user.setAddtime(new Date());
		System.out.println(user);
		qs.updateData(user);
		//这样也能跳转"WEB-INF/NewFile.jsp";这样不行showList();待优化
		list = qs.executeSQL("select * from user").getResultList(User.class);
		listStringUsers = list.toString();
		
		return "List.jsp";
	}
	
	/*返回适应DataTables使用的类似json的字符串*/
	@Method(value = "showList",isReturnPage = true)
	public String showList(){
		list = qs.executeSQL("select * from user order by addtime desc").getResultList(User.class);
		listStringUsers = list.toString();
		System.out.println(listStringUsers);
		return "List.jsp";
	}
	
	/*返回list*/
	@Method(value = "showList2",isReturnPage = false)
	public List<User> showList2(){
		log.log(Level.SEVERE, "showList2 is Enter...");
		list = qs.executeSQL("select * from user").getResultList(User.class);
		return list;
	}
	
	/*返回json字符串*/
	@Method("showList3")
	public String showList3(){
		list = qs.executeSQL("select * from user").getResultList(User.class);
		listUsers = JSON.toJSONString(list);//dataTables 不支持这样转过来的数据
		System.out.println(listUsers);
		return "List.jsp";
	}
	
	@Method(value="del",isReturnPage=true)
	public String showDel(@Param("id")String id){
		User user = new User();
		if(id != null){
			user.setId(Integer.parseInt(id));	
		}
		qs.deleteData(user);
		list = qs.executeSQL("select * from user order by addtime desc").getResultList(User.class);
		listStringUsers = list.toString();
		return "List.jsp";
	}
}
